package com.chuangyang.fixboxmaster.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Response;
import com.chuangyang.fixboxlib.widgets.PullDownRefreshListView;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.CashRequest;

import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.adapter.CashRepairRequestAdapter;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by vanshu on 15/8/6 13:47.
 */
public class CashRepairRequestFragment extends BaseFragment  implements PullDownRefreshListView.OnRefreshListener{
    private static final String TAG = CashRepairRequestFragment.class.getSimpleName();
    private View mContentView;
    public static final String REQUEST_TYPE = "request_type";
    public static final int CASH_INCOME_HISTORY = 1;
    public static final int CASH_REQUEST_HISTORY = 2;
    private CashRepairRequestAdapter mAdapter;
    private ArrayList<CashRequest> cashRequestList = new ArrayList<>();
    private PullDownRefreshListView mListView;
    private OnPullRefreshListener listener;
    public int mCurrentType;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mCurrentType = getArguments().getInt(REQUEST_TYPE, -1);
        mContentView = inflater.inflate(R.layout.repair_list_view, container, false);
        mListView = (PullDownRefreshListView) mContentView.findViewById(R.id.listview);
        mAdapter = new CashRepairRequestAdapter(cashRequestList);
        mListView.setAdapter(mAdapter);
        mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
        mListView.setOnRefreshListener(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
        loadData();
    }

    public boolean canCollectionViewScrollUp() {
        return ViewCompat.canScrollVertically(mListView, -1);
    }



    public ListView getListView() {
        return mListView;
    }

    @Override
    public void onRefresh() {
        loadData();
    }


    public void updateListView(OnPullRefreshListener listener) {
        this.listener = listener;
        cashRequestList.clear();
        mListView.setAdapter(mAdapter);
        loadDataWithoutFootLoading();
    }


    public void loadData() {
        mListView.setLoading();
        loadDataWithoutFootLoading();
    }

    public void loadDataWithoutFootLoading() {
        if (mCurrentType == CashRepairRequestFragment.CASH_INCOME_HISTORY) {
            addRequest(new GsonRequest<>(FixBoxApi.GET_CASH_INCOME_HISTORY, CashRequest.CashRequestInfo.class, responseListener));
        } else if (mCurrentType == CashRepairRequestFragment.CASH_REQUEST_HISTORY) {
            addRequest(new GsonRequest<>(FixBoxApi.GET_CASH_REQUEST_HISTORY, CashRequest.CashRequestInfo.class, responseListener));
        }
    }

    private Response.Listener<CashRequest.CashRequestInfo> responseListener=new Response.Listener<CashRequest.CashRequestInfo>() {
        @Override
        public void onResponse(CashRequest.CashRequestInfo response) {
            if(response.result.totalCount==0){
                setEmptyView();
                return ;
            }
            if (mCurrentType == CashRepairRequestFragment.CASH_INCOME_HISTORY) {
                cashRequestList.addAll(Arrays.asList(response.result.finishedOrders));
            } else if (mCurrentType == CashRepairRequestFragment.CASH_REQUEST_HISTORY) {
                cashRequestList.addAll(Arrays.asList(response.result.withorders));
            }
            mAdapter.notifyDataSetChanged();
            showResult();
            if (cashRequestList.size() >= response.result.totalCount)
                mListView.setLoadingEnd();
            else
                mListView.setLoadingIdle();
            if (listener != null) {
                listener.onPullRefresh();
            }
        }
    };

}
